package com.druid.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.druid.config.bean.DataSourceBean;
import com.druid.generator.util.ProjectPathInfo;
import com.druid.generator.util.UtilForGenerator;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Log4j2
public class InitDataBaseGenerator {

    public static void main(String[] args) throws Exception {
        //  获取maven项目信息
        List<ProjectPathInfo> projectPathInfoList = UtilForGenerator.getProjectPathInfo();
        int index0 = Integer.parseInt(UtilForGenerator.scanner("请选择模块:\n"+ UtilForGenerator.getIndexWithNameString(projectPathInfoList,ProjectPathInfo::getModuleName)));
        ProjectPathInfo projectPathInfo = projectPathInfoList.get(index0);

        //  读取数据源信息
        List<DataSourceBean> dataSourceBeanList = UtilForGenerator.getYmlPropery( projectPathInfo.getResourcePath() );
        dataSourceBeanList.stream()
                .peek(dataSourceBean ->
                        dataSourceBean
                                .setPackageMapperPath(projectPathInfo.getPackagePath()+".mapper."+dataSourceBean.getName().replaceAll("-","").trim()+"mapper")
                                .setPackageServicePath(projectPathInfo.getPackagePath()+".service."+dataSourceBean.getName().replaceAll("-","").trim()+"service")
                ).collect(Collectors.toList());


        // 模板引擎的路径
        String testPath = ((System.getProperty("user.dir") + "/management-server/common/" + "src/test/java/com/druid/")).replaceAll("\\\\","/"); //  生成动态数据源切换配置
        String templatePathDB = testPath + "generator/template/dataSourceSwitchAspect.java.ftl";
        String templatePathLog = testPath + "generator/template/log4j2.yml.ftl";//  生成日志文件

        String outPathDB = projectPathInfo.getAbsolutePath() + "/common/aspect/" + "DataSourceSwitchAspect" + StringPool.DOT_JAVA;
        String outPathLogDev = projectPathInfo.getResourcePath() + "/logging/log4j2-dev.yml";
        String outPathLogPro = projectPathInfo.getResourcePath() + "/logging/log4j2-prod.yml";
        String outPathLogTest = projectPathInfo.getResourcePath() + "/logging/log4j2-test.yml";

        //  将需要处理文件文件统一放到map集合中
        Map<String, String> fileMap = new HashMap();
        fileMap.put( outPathDB, templatePathDB );
        if ( !new File(outPathLogDev).exists() ) fileMap.put(outPathLogDev,templatePathLog);
        if ( !new File(outPathLogPro).exists() ) fileMap.put(outPathLogPro,templatePathLog);
        if ( !new File(outPathLogTest).exists() ) fileMap.put(outPathLogTest,templatePathLog);

        //组织需要输入到模板文件中的参数
        Map paramMap = new HashMap(1);
        Map unifyParam = new HashMap(); //  为了统一mybatis-plus的自动生成工具的参数，这里加了个容器用cfg作为键来装
        unifyParam.put("dbs",dataSourceBeanList);
        unifyParam.put("projectInfo",projectPathInfo);
        unifyParam.put("packageName",projectPathInfo.getPackagePath());
        unifyParam.put("UUID", UUID.randomUUID().getMostSignificantBits()+"L");
        paramMap.put("cfg",unifyParam);
        paramMap.put("author","druid-elf");
        paramMap.put("date", LocalDateTime.now().toString());

        //  开始生成Java类文件
        for (String outJavaFilePath : fileMap.keySet()) {

            String modelFilePath = fileMap.get(outJavaFilePath);
            if ( !UtilForGenerator.createJavaFileByModel(modelFilePath,outJavaFilePath,paramMap) ) {
                log.error(outJavaFilePath+"文件生成失败!");
                continue;
            }
            System.out.println(outJavaFilePath+"文件生成成功!");
        }
    }

}
